﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessCountry
    {
        #region Create
        public static void createCountry(Country c)
        {
            try
            {
                dataCountry.createCountry(c);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating country");
            }
        }

        public static void createCountry(String name)
        {
            try
            {
                Country c = new Country();
                c.Name = name;

                dataCountry.createCountry(c);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating country");
            }
        }

        public static void createCountryCoordinates(Country c, Dictionary<float, float> coords)
        {
            try
            {
                List<Coordinate> coordinates = new List<Coordinate>();

                foreach (KeyValuePair<float, float> co in coords)
                {
                    BusinessCoordinate.createCoordinateForCountry(co.Key, co.Value, c);
                }
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating coordinates for country");
            }
        }
        #endregion

        #region Get
        public static Country getCountry(Guid guid)
        {
            try
            {
                return dataCountry.getCountry(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting contry");
            }
        }

        public static List<Country> getAllCountries()
        {
            try
            {
                return dataCountry.getAllCountries().ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting all countries");
            }
        }

        public static List<Plant> getPlantByCountry(Country c)
        {
            try
            {
                List<Plant> plants = new List<Plant>();

                Expression<Func<Zone, bool>> whereClause = (Zone z) => (z.CountryID == c.CountryID);
                List<Zone> zones = dataZone.getZones(whereClause).ToList();

                List<Zones_Plant> zone_plants = new List<Zones_Plant>();
                Expression<Func<Zones_Plant, bool>> whereClauseZP; 

                foreach (Zone z in zones)
                {
                    whereClauseZP = (Zones_Plant zp) => (zp.ZoneID == z.ZoneID);
                    zone_plants.AddRange(dataZone.getZone_Plant(whereClauseZP).ToList());
                }

                foreach (Zones_Plant zp in zone_plants)
                {
                    Plant p = dataPlant.getPlant(zp.PlantID);

                    if (!plants.Contains(p))
                    {
                        plants.Add(p);
                    }
                }

                return plants;
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting plants by country");
            }
        }

        public static List<Coordinate> getCountryCoordiantes(Country c)
        {
            try
            {
                Expression<Func<Coordinate, bool>> whereClause = (Coordinate co) => (co.CountryID == c.CountryID);

                return dataCoordiante.getCoordinates(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting country coordinates");
            }
        }
        #endregion

        #region Set
        public static Boolean changeCountry(Zone z)
        {
            try
            {
                return dataZone.changeZone(z);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing zone");
            }
        }
        #endregion

        #region Delete
        #endregion
    }
}
